package com.halasa.backup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

public class Config {

    private static final Logger LOG = Logger.getLogger(Config.class.getName());
    
    private static Config instance;
    
    private static final String CONFIG_FILE_PARAM = "com.halasa.backup.configFile";
    private static final String SOURCE_DIR_PROPERTY = "source.dir";
    private static final String BACKUP_BASEDIR_PROPERTY = "backup.baseDir";
    private static final String BACKUP_INC_COUNT_PROPERTY = "backup.inc.count";
    private static final String EXCLUDE_DIRS_PROPERTY = "exclude.dirs";
    
    private final File sourceDir;
    private final File backupBaseDir;
    private final int incBackupCount;
    private String[] excludeDirs;
    
    public static Config getInstance() throws IOException {
        if (Config.instance == null) {
            Config.instance = new Config();
        }
        return Config.instance;
    }
    
    private Config() throws IOException {
        String configFileName = System.getProperty(CONFIG_FILE_PARAM);
        if (configFileName == null || configFileName.trim().length() == 0) {
            throw new IllegalStateException("No config file specified. Use -D" + CONFIG_FILE_PARAM + "=fileName Java parameter.");
        }
        
        File confingFile = new File(configFileName);
        if (! confingFile.isFile()) {
            throw new FileNotFoundException("Config file '" + confingFile.getAbsolutePath() + "' does not exist.");
        }
        
        Properties properties = new Properties();
        properties.load(new FileInputStream(confingFile));
        
        String sourceDirName = properties.getProperty(SOURCE_DIR_PROPERTY);
        String backupDirName = properties.getProperty(BACKUP_BASEDIR_PROPERTY);
        this.incBackupCount = Integer.parseInt(properties.getProperty(BACKUP_INC_COUNT_PROPERTY));
        String excludeDirsString = properties.getProperty(EXCLUDE_DIRS_PROPERTY);
        if (excludeDirsString != null && excludeDirsString.trim().length() > 0) {
            this.excludeDirs = excludeDirsString.split(File.pathSeparator);
        }
            
        LOG.info("--- Config ---");
        LOG.info("Source dir:       " + sourceDirName);
        LOG.info("Backup base dir:  " + backupDirName);
        LOG.info("Inc backup count: " + this.incBackupCount);
        LOG.info("Exclude dirs:     " + excludeDirsString);
        LOG.info("");
        
        this.sourceDir = new File(sourceDirName);
        this.backupBaseDir = new File(backupDirName);
        
        if (! this.sourceDir.isDirectory()) {
            throw new FileNotFoundException("Source path '" + this.sourceDir.getAbsolutePath() + "' is not a directory.");
        }
        if (! this.backupBaseDir.isDirectory()) {
            throw new FileNotFoundException("Backup path '" + this.backupBaseDir.getAbsolutePath() + "' is not a directory.");
        }
    }
    
    public File getSourceDir() {
        return sourceDir;
    }

    public File getBackupBaseDir() {
        return backupBaseDir;
    }

    public int getIncBackupCount() {
        return incBackupCount;
    }
    
    public String[] getExcludeDirs() {
        return excludeDirs;
    }
}
